package strukturaSviatky;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Sviatky {
    //public variables

    //private variables
    private boolean connected = false;
    private Connection con = null;
    private String url = "jdbc:mysql://kempelen.ii.fmph.uniba.sk:3306/";
    private String db = "vratnica";
    private String driver = "com.mysql.jdbc.Driver";
    private String user = "vratnica";
    private String pass = "MonikaVeronika";
    private ArrayList<Sviatok> sviatky; //ArrayList - zoznam sviatkov

    //public methods
    public Sviatky() throws SQLException, ParseException {
        if (connect()) {
            System.out.println("Pripojene k dtabaze MySQL.");
            sviatky = new ArrayList<>();
            loadSviatky();
        } else {
            System.out.println("Nepodarilo sa pripojit k databaze.");
        }
    }

    public boolean addSviatok(Sviatok sviatok) throws SQLException {
        //pridanie sviatku do databazy
        Statement st = con.createStatement();
        st.executeUpdate("INSERT INTO sviatky_table VALUES (null, '" + sviatok.getDatumSQL() + "', '"
                + sviatok.getPopis() + "')");

        sviatky.add(sviatok);
        return true;
    }

    public boolean editSviatok(int index, Sviatok sviatok) throws SQLException {
        //upravenie sviatku podla zvoleneho indexu - pozicie v Arrayliste
        if (index < sviatky.size()) {
            removeSviatok(index);
            addSviatok(sviatok);
            return true;
        }
        return false;
    }

    public boolean removeAll() {
        try {
            Statement st = con.createStatement();
            String deleteData = "DELETE FROM sviatky_table";
            st.executeUpdate(deleteData);

            sviatky.clear();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(Sviatky.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public boolean removeSviatok(int index) throws SQLException {
        //odstranenie sviatku podla zvoleneho indexu - pozicie v Arrayliste
        if (index < sviatky.size()) {
            Statement st = con.createStatement();
            String deleteData = "DELETE FROM sviatky_table WHERE datum = '" + sviatky.get(index).getDatumSQL() + "'";
            st.executeUpdate(deleteData);

            sviatky.remove(index);
            return true;
        }
        return false;
    }

    public boolean removeSviatok(String datum) throws SQLException, ParseException {
        //odstranenie sviatku podla zvoleneho datumu
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd.MM.yyyy");
        Calendar c = new GregorianCalendar();
        c.setTime(sdf1.parse(datum));
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");

        Statement st = con.createStatement();
        String deleteData = "DELETE FROM sviatky_table WHERE datum = '" + sdf2.format(c.getTime()) + "'";
        st.executeUpdate(deleteData);

        for (int i = 0; i < sviatky.size(); i++) {
            if (sviatky.get(i).getDatum().equals(datum)) {
                sviatky.remove(i);
            }
            return true;
        }
        return false;
    }

    public ArrayList<Sviatok> getSviatky(int rok, int mesiac) {
        //vrati zoznam sviatkov za zvoleny rok a mesiac - pouzivat iba na ziskanie dat, nie na upravu dat!    
        ArrayList<Sviatok> result = new ArrayList<>();

        for (int i = 0; i < sviatky.size(); i++) {
            if (sviatky.get(i).getMonth() == mesiac && sviatky.get(i).getYear() == rok) {
                result.add(sviatky.get(i));
            }
        }

        return result;
    }

    public ArrayList<Sviatok> getSviatky(int rok) {
        //vrati zoznam sviatkov za zvoleny rok - pouzivat iba na ziskanie dat, nie na upravu dat!
        ArrayList<Sviatok> result = new ArrayList<>();

        for (int i = 0; i < sviatky.size(); i++) {
            if (sviatky.get(i).getYear() == rok) {
                result.add(sviatky.get(i));
            }
        }

        return result;
    }

    public ArrayList<Sviatok> getSviatky() {
        //vrati zoznam vsetkych sviatkov - pouzivat iba na ziskanie dat, nie na upravu dat!
        return sviatky;
    }

    public boolean isConnected() {
        //vrati true ak je pripojene do databazy
        return connected;
    }

    //private methods
    private boolean connect() {
        try {
            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(url + db, user, pass);
            connected = true;
            return true;
        } catch (Exception ex) {
            Logger.getLogger(Sviatky.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    private void loadSviatky() throws SQLException, ParseException {
        Statement st = con.createStatement();

        ResultSet res = st.executeQuery("SELECT * FROM sviatky_table ORDER BY datum");
        while (res.next()) {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(res.getString("datum")));
            sviatky.add(new Sviatok(calendar, res.getString("popis")));
        }
    }
}
